package com.istudy.dao.xyjdao.impl;



import com.istudy.dao.xyjdao.BaseDao;
import com.istudy.dao.xyjdao.CommentInt;
import com.istudy.entity.Comment;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 评论表CRUP实现类，该实现类继承了BaseDao（实现了数据库基本通用的查询和增删改）
 * Commentimpl接口规定了该类的所有操作
 *
 * @author xdd
 * @create 2020-08-23 13:23
 */
public class CommentimplImpl extends BaseDao implements CommentInt {

    /**
     * 初始化查询需要的连接
     *
     * @param conn
     */
    public CommentimplImpl(Connection conn) {
        super(conn);
    }

    /**
     * 查询用户对应评论表所有数据
     *
     * @return 评论列表集合
     */
    @Override
    public List<Comment> selectCommentinformation(int pageIndex,int pageSize) throws SQLException {
        System.out.println(pageIndex);
        System.out.println(pageSize);
        int i=(pageIndex-1)*pageSize;
        List<Comment> commentList = new ArrayList<Comment>();//数据集合
        String sql = "SELECT \n" +
                " u.`user_name`,\n" +
                " c.*\n" +
                " FROM `comment` AS c,`user` AS u\n" +
                " WHERE c.`comment_user_id`=u.`user_id` limit "+i+","+pageSize+" ;\n";//sql查询语句，查询用户对应的评论信息
        ResultSet resultSet = super.executeQuery(sql, null);//根据sql执行查询操作，返回结果集
        while (resultSet.next()) {
            Comment comment = mapperObject(resultSet);//执行映射s
            commentList.add(comment);//追加映射数据到数据集合中
        }
        return commentList;
    }

    /**
     * 根据查询结果集（ResultSet），映射数据库对象
     *
     * @param rs 结果集对象
     * @return 映射对象
     */
    @Override
    public Comment mapperObject(ResultSet rs) throws SQLException {
        Comment comment=new Comment();
        comment.setCommentId(rs.getInt("comment_id"));
        comment.setCommentUserId(rs.getInt("comment_user_id"));
        comment.setCommentVideoId(rs.getInt("comment_video_id"));
        comment.setCommentDataTime(rs.getString("comment_data_time"));
        comment.setCommentContent(rs.getString("comment_content"));
        comment.setUserName(rs.getString("user_name"));
        return comment;
    }

    /**
     * 查询品讨论总行数
     *
     * @return 视频总行数
     */
    @Override
    public Integer selectVideoCount() throws SQLException {
        Integer commentCount=null;//总行数
        String sql="SELECT COUNT(*) AS c\n" +
                "FROM `comment`;";//数据库sql语句
        ResultSet resultSet = super.executeQuery(sql, null);
        if(resultSet.next()){
            commentCount=resultSet.getInt("c");
        }
        return commentCount;
    }

}
